package com.aiyige.base;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.track.TrackUtil;
import com.aiyige.utils.ToastX;
import com.aiyige.utils.dialog.LoadingDialog;
import com.vondear.rxtools.RxActivityTool;
import com.vondear.rxtools.RxKeyboardTool;

import org.greenrobot.eventbus.EventBus;

/**
 * <li>基础activity类
 * Created by xiachao on 2017/8/10.
 */
public class BaseActivity extends AppCompatActivity {
    LoadingDialog loadingDialog;

    private static Handler handler;

    private boolean autoRegisterEventBus = false;
    protected View titleLayout = null;
    private TextView titleText;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RxActivityTool.addActivity(this);
        loadingDialog = new LoadingDialog(this);
    }

    /**
     * 页面返回按钮,默认显示底线
     */
    protected void initTitleLayout(String name) {
        initTitleLayout(name,false);
    }

    /**
     * 页面返回按钮,默认显示底线
     */
    protected void initTitleLayout(int name) {
        initTitleLayout(name,false);
    }

    /**
     *
     * @param name title的name
     * @param visibleBottomLine true：显示底线 ，false：隐藏底线
     */
    protected void initTitleLayout(String name,boolean visibleBottomLine) {
        titleLayout = findViewById(R.id.back_layout);
        ImageView backView = (ImageView) findViewById(R.id.back_iv);
        if (titleLayout != null) {
            titleLayout.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
        if (backView != null) {
            backView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
        titleText = (TextView) findViewById(R.id.title_text);
        if (titleText != null && !isNull(name)) {
            titleText.setText(name);
        }
        View bottomLineView = findViewById(R.id.bottomline);
        if (bottomLineView != null) {
            bottomLineView.setVisibility(visibleBottomLine?View.VISIBLE:View.GONE);
        }
    }

    /**
     *
     * @param name title的name
     * @param visibleBottomLine true：显示底线 ，false：隐藏底线
     */
    protected void initTitleLayout(int name,boolean visibleBottomLine) {
        titleLayout = findViewById(R.id.back_layout);
        ImageView backView = (ImageView) findViewById(R.id.back_iv);
        if (titleLayout != null) {
            titleLayout.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
        if (backView != null) {
            backView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
        titleText = (TextView) findViewById(R.id.title_text);
        if (titleText != null && name > 0) {
            titleText.setText(name);
        }
        View bottomLineView = findViewById(R.id.bottomline);
        if (bottomLineView != null) {
            bottomLineView.setVisibility(visibleBottomLine?View.VISIBLE:View.GONE);
        }
    }

    protected void setTitleText(String name) {
        if (titleText != null && !isNull(name)) {
            titleText.setText(name);
        }
    }

    protected void setTitleText(int name) {
        if (titleText != null && name > 0) {
            titleText.setText(name);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            if (autoRegisterEventBus && EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().unregister(this);
            }
        } catch (Exception e) {

        }
        RxKeyboardTool.hideSoftInput(this);
        RxActivityTool.finishActivity(this);
    }

    public void showLoading() {
        if (this != null && !this.isFinishing()) {
            loadingDialog.showLoading();
        }
    }

    public void showLoading(String msg) {
        if (this != null && !this.isFinishing()) {
            loadingDialog.setLoadText(msg);
            loadingDialog.showLoading();
        }
    }

    public void hideLoading() {
        if (this != null && !this.isFinishing()) {
            loadingDialog.dismiss();
        }
    }

    /**
     * 自动注册和解除EventBus
     *
     * @param register
     */
    protected void setAutoRegisterEventBus(boolean register) {
        autoRegisterEventBus = register;
    }

    @Override
    protected void onStart() {
        super.onStart();
        try {
            if (autoRegisterEventBus && !EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().register(this);
            }
        } catch (Exception e) {

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        TrackUtil.onPageStart(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        TrackUtil.onPageEnd(this);
    }



    protected boolean isNull(String string) {
        return TextUtils.isEmpty(string);
    }

    public boolean isEmpty(CharSequence s) {
        return TextUtils.isEmpty(s);
    }

    public boolean notEmpty(CharSequence s) {
        return !TextUtils.isEmpty(s);
    }

    protected void showToast(String string) {
        ToastX.show(this, string);
    }
}
